package cn.hinglo.gameclaw.admin.action;

import cn.hinglo.gameclaw.admin.service.MoneyService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.user.bean.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * @createTime: 2018/1/4
 * @author: HingLo
 * @description: 充值的金钱管理
 */
@RestController
@RequestMapping("admin/mo/")
public class MoneyAction extends BaseController {
    private final MoneyService moneyService;

    @Autowired
    public MoneyAction(MoneyService moneyService) {
        this.moneyService = moneyService;
    }

    /**
     * 查询全部的充值类型
     *
     * @return 充值类型的列表
     */
    @GetMapping("list")
    public Result listMoney() {
        return moneyService.listMoneyService();
    }

    /**
     * 添加一个充值类型
     *
     * @param money         充值类型对象
     * @param bindingResult 错误绑定结果信息
     * @return 返回添加结果
     */
    @PostMapping("add")
    public Result addMoney(@Valid Money money, BindingResult bindingResult, String eDate, String sDate, @RequestParam("image") MultipartFile image) {
        if (bindingResult.hasErrors()) {
            return error(bindingResult.getFieldError().getDefaultMessage());
        }
        return moneyService.addMoneyService(money, eDate, sDate, image);
    }

    /**
     * 删除某个充值选项
     *
     * @param id 充值类型的ID
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public Result deleteMoney(@RequestParam("id") String id) {
        return moneyService.deleteMoneyService(id);
    }

    /**
     * 修改金到账的
     *
     * @param id   充值的id
     * @param gold 修改后到账的金额
     * @return 返回修改结果
     */
    @PutMapping("update")
    public Result updateMoney(@RequestParam("id") String id, @RequestParam("gold") Integer gold, @RequestParam("giveGold") Integer giveGold) {
        return moneyService.updateMoneyService(id, gold, giveGold);
    }

    /**
     * 修改充值的背景图
     *
     * @param id         充值的Id
     * @param background 背景图对象
     * @return 返回从操作结果
     */
    @PostMapping("background")
    public Result updateBackground(@RequestParam("id") String id, @RequestParam("background") MultipartFile background) {
        return moneyService.updateBackgroundService(id, background);
    }

    /**
     * 修改状态
     *
     * @param id     充值Id
     * @param status 修改的状态
     * @return 修改的操作结果
     */
    @PutMapping("status")
    public Result updateStatus(@RequestParam("id") String id, boolean status) {
        return moneyService.updateStatusService(id, status);
    }

}
